package com.example.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Q:在同一个service（目标对象）上，对被嵌套的方法使用AOP注解不生效？
 *   存在method1和method2,method1调用method2,且method2使用AOP注解。结果aop不生效
 *   当method1使用AOP注解的时候，aop生效。
 * A:
 *
 */
@Aspect
@Order(1)
@Component
public class AopLockAspect {
    private static final Logger logger = LoggerFactory.getLogger(AopLockAspect.class);
    // 默认false 互斥锁
    private static final Lock lock = new ReentrantLock();
//    @Pointcut(value = "@annotation(AopLock)")
//    public void aopLock(){}


    @Around("@annotation(AopLock)")
    public Object executeAop(ProceedingJoinPoint point) throws Throwable {
        Object o = null;
        lock.lock();
        o = point.proceed();
        lock.unlock();
        logger.info("thread:" + Thread.currentThread().getId() + ", method:" + point.getSignature().getName());
        return o;
    }
}
